import {IMock} from './i-mock';
import {MockConfigItem} from '../../entity/mock-config-item';
import {MockUtils} from '../mock-utils';
import {AlertMessage} from '../../entity/alert-message';

export class NumberMock implements IMock {
    getBaseTypeMockConfigDisplay(mockConfigItem: MockConfigItem): string {
        switch (mockConfigItem.mockType) {
            case 1:
                return '@random(' + mockConfigItem.mockValueStart + ', ' + mockConfigItem.mockValueEnd + ')';
            case 2:
                return '@random(list)';
            default:
                return mockConfigItem.value;
        }
    }

    onSave(mockConfigItem: MockConfigItem): boolean {
        if (mockConfigItem.mockType === 1) {
            // 某数值范围内随机
            let result = MockUtils.checkNumber(mockConfigItem.mockValueStart, Number.MIN_VALUE, null, '某数值范围内随机-开始值，');
            if (!result) {
                return false;
            }
            result = MockUtils.checkNumber(mockConfigItem.mockValueEnd, Number.MIN_VALUE, null, '某数值范围内随机-结束值，');
            if (!result) {
                return false;
            }
            if (mockConfigItem.mockValueStart > mockConfigItem.mockValueEnd) {
                MockUtils.setMockInfo(AlertMessage.error('某数值范围内随机，起始值不能大于结束值！'));
                return false;
            }
        }
        if (mockConfigItem.mockType === 2) {
            // 给定数值内随机
            let success = true;
            mockConfigItem.mockValueRandomList.forEach(item => {
                if (item.trim() === '') {
                    return;
                }
                if (isNaN(+item)) {
                    MockUtils.setMockInfo(AlertMessage.error('给定的不是数值：' + item));
                    success = false;
                }
            });
            return success;
        }
        return true;
    }
}
